package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.adapter.PlayListAdapter;
import com.example.myapplication.adapter.SongListAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.PlayList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SongListActivity extends AppCompatActivity {
    private Context context;
    private RecyclerView recyclerView;
    private List<PlayList.TracksBean> tracks;
    private PlayListAdapter adapter;
    private String songListID;
    private PlayList playList;
    private TextView tvTitle;
    private TextView tvDec;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_list);
        context = SongListActivity.this;

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new PlayListAdapter();
        recyclerView.setAdapter(adapter);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        songListID = bundle.getString("songListID");

        loadData(songListID);

        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(context, PlayActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putString("songID", tracks.get(position).getId());
                bundle1.putString("songName",tracks.get(position).getName());
                bundle1.putInt("albumID", tracks.get(position).getAl().getId());
                bundle1.putString("singerName", tracks.get(position).getAr().get(0).getName());
                intent.putExtras(bundle1);
                startActivity(intent);
            }
        });
    }

    private void loadData(String id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponse> call = api.getSongList(id);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                playList = baseResponse.getPlaylist();
                tracks = playList.getTracks();
                initView();
                tvTitle.setText(playList.getName());
                tvDec.setText(playList.getDescription());

                Glide.with(context)
                        .load(playList.getCoverImgUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                        .into(img);
                adapter.setData(tracks);
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void initView(){
        tvTitle = findViewById(R.id.tv_tittle);
        tvDec = findViewById(R.id.tv_dec);
        img = findViewById(R.id.rank_cover);
    }
}